/** * 配置管理 文件配置 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/interfileconfig/InterfileConfig' }"
        >文件配置</el-breadcrumb-item
      >
      <el-breadcrumb-item>字段配置</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input
          size="small"
          v-model="formInline.fieldName"
          placeholder="请输入字段名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          size="small"
          v-model="formInline.fieldType"
          placeholder="请选择字段类型"
        >
          <el-option
            v-for="fieldType in fieldTypeList"
            :label="fieldType.key"
            :value="fieldType.value"
            :key="fieldType.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleEdit('', null, addFlag)"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <!--文件配置列表-->
    <el-table
      size="small"
      :data="fieldListData"
      highlight-current-row
      v-loading="loading"
      border
      stripe
      element-loading-text="拼命加载中"
      style="width: 100%;"
    >
      <el-table-column
        prop="id"
        label="字段ID"
        width="150"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="fileId"
        label="文件ID"
        width="150"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="fieldName"
        label="字段名称"
        width="150"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="fieldType"
        label="字段类型（0数据字段，1校验字段）"
        width="150"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="listFieldAttr(scope.row)"
            >属性</el-button
          >
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row, editFlag)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="deleteConfigInfo(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
    <!-- 添加 编辑界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="55%"
      @click="closeDialog('editForm')"
    >
      <el-form
        :inline="true"
        label-width="120px"
        :model="editForm"
        :rules="rules"
        ref="editForm"
      >
        <el-form-item label="字段名称" prop="fieldName">
          <el-input
            size="small"
            v-model="editForm.fieldName"
            auto-complete="off"
            placeholder="请输入字段名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="字段类型" prop="fieldType">
          <el-select
            size="small"
            v-model="editForm.fieldType"
            placeholder="请选择字段类型"
            class="userRole"
            style="width: 200px;"
          >
            <el-option
              v-for="fieldType in fieldTypeList"
              :label="fieldType.key"
              :value="fieldType.value"
              :key="fieldType.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog('editForm')"
          >取消</el-button
        >
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('editForm')"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  FieldConfigList,
  FieldConfigAdd,
  FieldConfigUpdate,
  FieldConfigDelete
} from "../../api/interfileMG";
import Pagination from "../../components/Pagination";
export default {
  data() {
    return {
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      editFlag: "EDIT",
      addFlag: "ADD",
      isEdit: false,
      title: "添加字段信息",
      fieldTypeList: [
        { key: "请选择", value: "" },
        { key: "数据文件字段", value: "0" },
        { key: "校验文件字段", value: "1" }
      ],
      editForm: {
        id: "",
        fileId: "",
        fieldName: "",
        fieldType: "",
        token: localStorage.getItem("logintoken")
      },
      // rules表单验证
      rules: {
        fieldName: [
          { required: true, message: "请输入字段类型", trigger: "blur" }
        ],
        fieldType: [
          { required: true, message: "请选择字段类型", trigger: "blur" }
        ]
      },
      formInline: {
        page: 1,
        limit: 10,
        token: localStorage.getItem("logintoken"),
        fileId: "",
        fieldName: "",
        fieldType: ""
      },
      userparm: [], //搜索权限
      // 字段数据
      fieldListData: [],
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    };
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline);
  },

  mounted() {
    this.formInline.fileId = this.$route.query.fileId;
  },
  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取配置列表
    getdata(parameter) {
      this.loading = true;
      /***
       * 调用接口
       */
      this.loading = true;
      FieldConfigList(parameter)
        .then(res => {
          this.loading = false;
          if (res.code == "000000") {
            this.fieldListData = res.data;
            // 分页赋值
            this.pageparm.currentPage = this.formInline.page;
            this.pageparm.pageSize = this.formInline.limit;
            this.pageparm.total = res.count;
          } else {
            this.$message({
              type: "info",
              message: res.message
            });
          }
        })
        .catch(err => {
          this.loading = false;
          this.$message.error("文件字段加载失败，请稍后再试！");
        });
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      this.getdata(this.formInline);
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline);
    },
    listFieldAttr(row) {
      this.$router.push({
        path: "/fieldattrconfig/FieldAttrConfig",
        query: { fileId: row.fileId, fieldId: row.id }
      });
    },
    //显示编辑界面
    handleEdit: function(index, row, handleType) {
      this.editFormVisible = true;
      if (handleType == this.editFlag) {
        this.isEdit = true;
        if (row != undefined && row != "undefined") {
          this.title = "修改字段信息";
          this.editForm.id = row.id;
          this.editForm.fileId = row.fileId;
          this.editForm.fieldName = row.fieldName;
          this.editForm.fieldType = row.fieldType;
        }
      } else {
        this.isEdit = false;
        this.title = "添加字段信息";
        this.editForm.id = "";
        this.editForm.fileId = this.formInline.fileId;
        this.editForm.fieldName = "";
        this.editForm.fieldType = "";
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          if (this.isEdit) {
            this.updateConfigInfo();
          } else {
            this.addConfigInfo();
          }
        } else {
          return false;
        }
      });
    },
    // 更新配置信息
    updateConfigInfo() {
      FieldConfigUpdate(this.editForm)
        .then(res => {
          this.editFormVisible = false;
          this.loading = false;
          if (res.code == "000000") {
            this.getdata(this.formInline);
            this.$message({
              type: "success",
              message: "字段配置信息更新成功！"
            });
          } else {
            this.$message({
              type: "info",
              message: res.message
            });
          }
        })
        .catch(err => {
          this.editFormVisible = false;
          this.loading = false;
          this.$message.error("字段配置信息更新失败，请稍后再试！");
        });
    },
    // 新增配置信息
    addConfigInfo() {
      FieldConfigAdd(this.editForm)
        .then(res => {
          this.editFormVisible = false;
          this.loading = false;
          if (res.code == "000000") {
            this.getdata(this.formInline);
            this.$message({
              type: "success",
              message: "字段配置信息新增成功！"
            });
          } else {
            this.$message({
              type: "info",
              message: res.message
            });
          }
        })
        .catch(err => {
          this.editFormVisible = false;
          this.loading = false;
          this.$message.error("字段配置信息新增失败，请稍后再试！");
        });
    },
    // 删配置信息
    deleteConfigInfo(index, row) {
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          FieldConfigDelete(row.id)
            .then(res => {
              if (res.code == "000000") {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                this.getdata(this.formInline);
              } else {
                this.$message({
                  type: "info",
                  message: res.message
                });
              }
            })
            .catch(err => {
              this.loading = false;
              this.$message.error("文件配置信息删除失败，请稍后再试！");
            });
        })
        .catch(err1 => {
          console.log(err1);
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 关闭编辑、增加弹出框
    closeDialog(formName) {
      this.editFormVisible = false;
      this.$refs[formName].resetFields();
    },
    // 添加字段
    addField(index, fieldRow) {
      this.addFieldFormVisible = true;
    },
    // 属性弹出框
    closeFieldDialog(formName) {
      this.addFieldFormVisible = false;
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>
